package com.example.ggpicturegg.Mysql_com;

import android.graphics.Bitmap;
import android.util.Log;

import com.example.ggpicturegg.tool.BlobAndBytes;
import com.mysql.jdbc.Blob;
import com.mysql.jdbc.PreparedStatement;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/*
        用户数据库操作类
        实现用户的 CRUD 操作
 */
public class ImageDao extends DbOpenHelper{


    public ImageDao() {
    }

    // 查询 所有的照片 信息
    public List<ImageInfo> getAllUserList(){
        List<ImageInfo> list= new ArrayList<>();
        try{
            getConnection();  //尝试连接
            String sql1="select * from imageinfo";
            String sql=new String(sql1.getBytes("UTF-8"), "ISO-8859-1");
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while(rs.next()){
                ImageInfo item = new ImageInfo();
                item.setId(rs.getInt("id"));
                item.setPicture_name(rs.getString("image_name"));
                item.setX_name(rs.getString("user_name"));
                item.setX_title(rs.getString("title"));
                item.setX_coutn(rs.getInt("collection"));
                item.setDesc(rs.getString("describe"));
                list.add(item);
            }
            Log.d("getConnection()", "True ");

        }catch (Exception e){
            Log.d("getUserByUnameAndUpass", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        Log.d("用户个数list............", new String(String.valueOf(list.size())));
        return list;
    }


    // 查询 朋友的照片 信息
    public List<ImageInfo> getUserFriendList(String name){
        List<ImageInfo> list= new ArrayList<>();
        try{
            getConnection();  //尝试连接
            String sql1="SELECT id,image_name,user_name,title,collection,`describe` from imageinfo,friendinfo WHERE friendinfo.user_friend_name1='"+name+"' AND friendinfo.user_friend_name2=imageinfo.user_name";
            String sql=new String(sql1.getBytes("UTF-8"), "ISO-8859-1");
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            System.out.println(name + "-----------------------------------------------------");
            System.out.println(sql1);
            rs = pStmt.executeQuery();
            while(rs.next()){
                ImageInfo item = new ImageInfo();
                item.setId(rs.getInt("id"));
                item.setPicture_name(rs.getString("image_name"));
                item.setX_name(rs.getString("user_name"));
                item.setX_title(rs.getString("title"));
                item.setX_coutn(rs.getInt("collection"));
                list.add(item);
            }
            Log.d("getConnection()", "True ");

        }catch (Exception e){
            Log.d("getUserByUnameAndUpass", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        Log.d("用户个数list............", new String(String.valueOf(list.size())));
        return list;
    }

    // 查询 关注的人的照片 信息
    public List<ImageInfo> getUserCareList(String name){
        List<ImageInfo> list= new ArrayList<>();
        try{
            getConnection();  //尝试连接
            String sql1="SELECT id,image_name,user_name,title,collection,`describe` from imageinfo,careinfo WHERE careinfo.user_care_name1='"+name+"' AND careinfo.user_care_name2=imageinfo.user_name;";
            String sql=new String(sql1.getBytes("UTF-8"), "ISO-8859-1");
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            System.out.println(name + "-----------------------------------------------------");
            System.out.println(sql1);
            rs = pStmt.executeQuery();
            while(rs.next()){
                ImageInfo item = new ImageInfo();
                item.setId(rs.getInt("id"));
                item.setPicture_name(rs.getString("image_name"));
                item.setX_name(rs.getString("user_name"));
                item.setX_title(rs.getString("title"));
                item.setX_coutn(rs.getInt("collection"));
                list.add(item);
            }
            Log.d("getConnection()", "True ");

        }catch (Exception e){
            Log.d("getUserByUnameAndUpass", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        Log.d("用户个数list............", new String(String.valueOf(list.size())));
        return list;
    }

    // 查询 喜欢的照片 信息 点赞之后的照片
    public List<ImageInfo> getUserLikeList(String name){
        List<ImageInfo> list= new ArrayList<>();
        try{
            getConnection();  //尝试连接
            String sql1="SELECT id,image_name,user_name,title,collection,`describe` from imageinfo,likeinfo WHERE likeinfo.user_like_name='"+name+"' AND likeinfo.image_like_id=imageinfo.id;";
            String sql=new String(sql1.getBytes("UTF-8"), "ISO-8859-1");
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            System.out.println(name + "-----------------------------------------------------");
            System.out.println(sql1);
            rs = pStmt.executeQuery();
            while(rs.next()){
                ImageInfo item = new ImageInfo();
                item.setId(rs.getInt("id"));
                item.setPicture_name(rs.getString("image_name"));
                item.setX_name(rs.getString("user_name"));
                item.setX_title(rs.getString("title"));
                item.setX_coutn(rs.getInt("collection"));
                list.add(item);
            }
            Log.d("getConnection()", "True ");

        }catch (Exception e){
            Log.d("getUserByUnameAndUpass", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        Log.d("用户个数list............", new String(String.valueOf(list.size())));
        return list;
    }

    // 查询 单个的用户 信息
    public List<ImageInfo> getOneUserList(String name){
        List<ImageInfo> list= new ArrayList<>();
        try{
            getConnection();  //尝试连接
            String sql1="select * from imageinfo where user_name='"+name+"'";
            String sql=new String(sql1.getBytes("UTF-8"), "ISO-8859-1");
            Log.d("mysql", sql);
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while(rs.next()){
                ImageInfo item2 = new ImageInfo();
                item2.setId(rs.getInt("id"));
                item2.setPicture_name(rs.getString("image_name"));
                item2.setX_title(rs.getString("title"));
                item2.setX_name(rs.getString("user_name"));
                item2.setX_coutn(rs.getInt("collection"));
                list.add(item2);
            }
            Log.d("getConnection()", "True ");

        }catch (Exception e){
            Log.d("getUserByUnameAndUpass", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
        }
        Log.d("用户个数list............", new String(String.valueOf(list.size())));
        return list;
    }

    // 添加照片
    public int addImage(ImageInfo item){
        int iRow = 0;
        try{
            getConnection();
            String sql1="insert into imageinfo values(?,?,?,?,?,?)";
            String sql=new String(sql1.getBytes("UTF-8"), "ISO-8859-1");
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            pStmt.setInt(1,item.getId());
            pStmt.setString(2,item.getPicture_name());
            pStmt.setString(3,item.getX_name());
            pStmt.setString(4,item.getX_title());
            pStmt.setInt(5,item.getX_coutn());
            pStmt.setString(6,item.getDesc());
            iRow = pStmt.executeUpdate();
            Log.d("addImage is    ok ", "addImage:  is ok");
        }catch (Exception e){
            Log.d("addImage14_axuelixueli", "error ");
            e.printStackTrace();
        }finally {
            clossAll();  //关闭所有连接
            System.out.println("--------- 关闭所有连接----------is ---------- ok");
        }
        return iRow;    // 返回 ： 影响的行数
    }

    //获取用户的个数
    public int getUserNumber(){
        int count = 0 ; // 用户的数量
        try{
            getConnection(); //尝试连接
            String sql = "select count(1) as sl from imageinfo";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while(rs.next()){
                count = rs.getInt("sl");
            }
        }catch (Exception e){
            Log.d("MysqlHelp", "ERROR");
            e.printStackTrace();

        }
        return count;
    }

    //获取用户的id最大值
    public int getImage_max_id(){
        int max_id = 0;
        try{
            getConnection(); //尝试连接
            String sql = "SELECT id as sl FROM imageinfo ORDER BY id DESC LIMIT 0,1";
            pStmt = (PreparedStatement) conn.prepareStatement(sql);
            rs = pStmt.executeQuery();
            while(rs.next()){
                max_id = rs.getInt("sl");
            }

        }catch (Exception e){
            Log.d("getImage_max_id", "getImage_max_id:  is  error");
            e.printStackTrace();
        }
        String ss = new String(""+max_id);
        Log.d("max_id_value_is  ", ss);
        return max_id;
    }

}
